<?php

namespace App\Services\AttendUser;

use Closure;
use App\Models\User;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Api\V1\Requests\FormRequest;
use App\Services\UserFromInviteCode\UserFromInviteCode;

class AttendUserRequest extends FormRequest
{
	protected User $user;

	public function rules(): array
	{
		return [
			// Only for binding parent, user cannot change own invite_code
			'attention_user_id' =>  ['required', 'integer', $this->userAttention()],
		];
	}

	public function userAttention(): Closure
	{
		return function ($key, $value, $fail) {

			$attentionUser = User::find($value); 
			$attentionRecord = $this->user()->attentions->where('attention_user_id',$value)->first();

            if (!$attentionUser) {
                return $fail('关注用户不存在');
            } elseif ($attentionRecord) {
				return $fail('已关注该用户');
			} elseif ($value == $this->user()->id) {
				return $fail('不可关注自己');
			}
			return true;
		};
	}
}
